<?php

/*
 *  Copyright (C) 2010 Emanuele Colonnelli <emanuele.colonnelli@phpstruct.com>
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

namespace org\phpstruct\core;
use org\phpstruct\core\driver;
use org\phpstruct\core\dto;

/**
 * Description of Error
 *
 * @author Emanuele Colonnelli <emanuele.colonnelli@phpstruct.com>
 */
class Error {
    const ERROR_TYPE_ERROR = 256;
    const ERROR_TYPE_WARNING = 512;
    const ERROR_TYPE_NOTICE = 1024;
    const ERROR_TYPE_DEPRECATED = 2048;

    const E_XML_PARSER_NOT_VALID = -10001;
    const E_XML_XMAP_NOT_FOUND = -10002;
    const E_XML_XMAP_VALIDATION = -10003;
    const E_XML_NOT_YET_LOADED = -10004;
    const
    E_CACHE_GENERIC = -20000,
    E_CACHE_DRIVER_GENERIC = -20001,
    E_CACHE_DRIVER_NOT_FOUND = -20002,
    E_CACHE_NULL_KEYSTRING = -20000;

    const
    E_LOADER_GENERIC = -30000,
    E_LOADER_FILENOTFOUND = -30001,
    E_LOADER_PARAM_VALIDATION = -30002;

    const
    E_CONF_FILENOTFOUND = -40001;


    public static $MSG = array();
    protected static $instance;
    /*
     * error\DefaultErrorDriver
     */
    protected static $errorHandler = 'org\phpstruct\core\driver\error\DefaultErrorDriver';

    protected function __construct() {
        self::$instance = $this;
        self::$MSG = array(
            self::E_XML_PARSER_NOT_VALID => _("Invalid XML Parser"),
            self::E_XML_NOT_YET_LOADED => _("xmap.xml is not yet loaded"),
            self::E_XML_XMAP_NOT_FOUND => _("Could not find xmap.xml"),
            self::E_XML_XMAP_VALIDATION => _("XMAP validation failed"),
            self::E_CACHE_GENERIC => _("Generic cache error"),
            self::E_CACHE_DRIVER_GENERIC => _("Generic cache driver error"),
            self::E_CACHE_DRIVER_NOT_FOUND => _("Could not find required cache driver"),
            self::E_CACHE_NULL_KEYSTRING => _("Cache key could not be an empty string"),
            self::E_LOADER_FILENOTFOUND => _("File not found"),
            self::E_LOADER_GENERIC => _("Generic Loader Error"),
            self::E_LOADER_PARAM_VALIDATION => _("Missing required parameters"),
        );

        if(Config::get('error.error_handler') != null){
            self::$errorHandler = Config::get('error.error_handler');
        }
    }

    /**
     * @return Error
     */
    public static function getInstance() {
        return (self::$instance instanceof org\phpstruct\core\Error) ? self::$instance : new self();
    }

    public function register($strErrorHandler) {
        self::$errorHandler = $strErrorHandler;
    }

    public function handle(exceptions\StructException $error){
        $r = new \ReflectionClass(self::$errorHandler);
        $eh = $r->newInstance();
        $eh->handleError($error);
    }

    public static function __callStatic($error, $args) {
        $instance = self::getInstance();
        $r = new \ReflectionClass(__CLASS__);
        $errorcode = $r->getConstant($error);
        if (isset(self::$MSG[$errorcode])) {
            return self::$MSG[$errorcode];
        } else {
            \trigger_error(_("Undefined error code: ") . $errorcode, \E_USER_ERROR);
            return null;
        }
    }

//
}